<template>
  <div>
    <div style="text-align:left">
      患者信息：
      <el-tag>姓名：{{patient.real_name}}</el-tag>
      <el-tag>病历号：{{patient.case_number}}</el-tag>
      <el-tag>年龄：{{patient.age}}</el-tag>
      <el-tag>性别：{{patient.gender}}</el-tag>
    </div>
    <el-divider></el-divider>
    <div style="font-size:20px;text-align:left">
      <i class="el-icon-document-checked">开设处方</i>
    </div>
    <el-divider></el-divider>
    <div style="display:flex">
      <el-button type="primary" disabled icon="el-icon-price-tag">处方金额：{{ total_price }}元</el-button>
    </div>
    <el-table :data="patient_drug_select_table" @selection-change="drugSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="expand" width="55">
        <template slot-scope="scope">
          <el-tag style="margin-left:110px">药品编码：{{scope.row.drug_code}}</el-tag>
          <el-tag style="margin-left:10px">包装单位：{{scope.row.drug_unit}}</el-tag>
          <el-tag style="margin-left:10px">生产厂家：{{scope.row.manufacturer}}</el-tag>
          <el-tag style="margin-left:10px">药剂类型：{{scope.row.drug_dosage}}</el-tag>
          <el-tag style="margin-left:10px">药品类型：{{scope.row.drug_type}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="药品名称" prop="drug_name" width="280"></el-table-column>
      <el-table-column label="药品规格" prop="drug_format"  width="160"></el-table-column>
      <el-table-column label="单价" prop="drug_price" width="100"></el-table-column>
      <el-table-column label="用法用量">
        <template slot-scope="scope">
          <el-input type="textarea" :rows="1" v-model="scope.row.drug_usage" placeholder="输入用法用量，使用频次等信息"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="数量" width="200">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.drug_number" :min="1" :max="100"
            :step="1" step-strictly @change="drugNumberChange"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column width="180">
        <template slot="header" slot-scope="scope">
          <el-button type="text" @click="delDrugFromTable()">删除</el-button>
          <el-button type="text" @click="drugDialogVisibale=true">增加</el-button>
        </template>
      </el-table-column>

    </el-table>
    <div style="text-align:left;margin-top:20px">
      <el-button type="primary" @click="addDrugPrescription()">开立处方</el-button>
      <el-button type="primary" @click="resetDrugInput()">重置处方</el-button>
    </div>

    <!-- 药品选择对话框 -->
    <el-dialog title="添加药品" :visible.sync="drugDialogVisibale">
      <div>
        <el-tag style="width:20%">药品名称:</el-tag><el-input style="width:80%" v-model="drug_search.drug_name" placeholder="请输入药品名称（支持模糊查询）"></el-input>
      </div>
      <div>
        <el-tag style="width:20%">拼音助记码:</el-tag><el-input style="width:80%" v-model="drug_search.mnemonic_code" placeholder="请输入药品拼音助记码（支持模糊查询）"></el-input>
      </div>
      <div style="text-align:right">
        <el-button @click="searchDrug()">搜索</el-button>
      </div>
      <el-table :data="view_search_drug_table" @selection-change="drugSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="药品编码" prop="drug_code" width="150"></el-table-column>
        <el-table-column label="药品名称" prop="drug_name"></el-table-column>
        <el-table-column label="药品规格" prop="drug_format" width="100"></el-table-column>
        <el-table-column label="药品单价" prop="drug_price" width="100"></el-table-column>
        <el-table-column label="包装单位" prop="drug_unit" width="100"></el-table-column>
        <el-table-column label="生产厂家" prop="manufacturer" width="150"></el-table-column>
      </el-table>
      <div>
        <el-button type="primary" @click="addSelectionDrug()">添加</el-button>
        <el-button type="info" @click="closeDialog()">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import qs from 'qs'
import {isNullMessage} from "../../../util/util"
export default {
  methods:{
    //开设处方:病历id 、药品id、用法用量、数量
    addDrugPrescription(){      
      for(let i=0;i<this.patient_drug_select_table.length;i++){
        let temp_pre = {};
        temp_pre["register_id"] = this.patient.id;
        temp_pre["drug_id"] = this.patient_drug_select_table[i].id;
        temp_pre["drug_usage"] = this.patient_drug_select_table[i].drug_usage;
        temp_pre["drug_number"] = this.patient_drug_select_table[i].drug_number;
        this.submit_prescription.push( temp_pre );
      }
      this.$http.post("http://localhost:8092/addPrescription",this.submit_prescription).then(
        (resp)=>{
          this.$message({
            message:resp.data.msg,
            type:"success"
          });
          this.patient_drug_select_table = [];
          this.math_total_pricr();
        }
      )
    },
    //重置处方输入
    resetDrugInput(){
      if(this.patient_drug_select_table!=null && this.patient_drug_select_table.length>0){
        for(let i=0;i<this.patient_drug_select_table.length;i++){
          this.$set(this.patient_drug_select_table[i],"drug_number",1);
          this.$set(this.patient_drug_select_table[i],"drug_usage","");
        }
      }      
    },
    //从已经选择的药品列表中，药品数量改变时触发
    drugNumberChange(){
      this.math_total_pricr();
    },
    //从已经选择的药品列表中，删除选中的药品行
    delDrugFromTable(){
      let oldVal = this.patient_drug_select_table;//表格已有数据
      let delVal = this.tempDrug;//想要删除的数据
      if((oldVal!=null && oldVal.length>0)&&(delVal!=null && delVal.length>0)){
        for(let i=0;i<delVal.length;i++){
          for(let j=0;j<oldVal.length;j++){
            if(oldVal[j].drug_code == delVal[i].drug_code){
              oldVal.splice(j,1);
              j--;
            }
          }
        }
      }
      this.math_total_pricr();      
    },
    //关闭对话框
    closeDialog(){
      this.clearDialog();
      this.drugDialogVisibale=false;
    },
    //对话框添加药品
    addSelectionDrug(){
      let sd = this.tempDrug;
      if(sd!=null && sd.length >0){
        for(let i=0;i<sd.length;i++){
          this.$set(sd[i],"drug_number",1);
          // sd[i]["drug_number"] = 1;
          this.patient_drug_select_table.push(sd[i]);
        }
      }
      this.clearDialog();
      this.math_total_pricr();
    },
    //计算处方药品金额
    math_total_pricr(){
      this.total_price = 0;
      if(this.patient_drug_select_table!=null && this.patient_drug_select_table.length>0){
        for(let i=0;i<this.patient_drug_select_table.length;i++){
          this.total_price = parseFloat(this.patient_drug_select_table[i].drug_price)
            *parseInt(this.patient_drug_select_table[i].drug_number) + this.total_price ;
        }
      }
      this.total_price = this.total_price.toFixed(2);
    },
    //清空对话框中已经输入的数据
    clearDialog(){
      this.drug_search = {};
      this.view_search_drug_table = [];
    },
    //对话框搜索药品
    searchDrug(){
      this.$http.post("http://localhost:8092/searchDrug",qs.stringify(this.drug_search)).then(
        (resp)=>{
          this.view_search_drug_table = resp.data;
        }
      )
    },
    //复选数据改变
    drugSelectionChange(val){
      this.tempDrug = val;
    }
  },
  data(){
    return{
      submit_prescription:[],//提交的处方数据
      tempDrug:[],//复选数据
      drugDialogVisibale:false,//药品选择对话框是否显示
      patient:{}, //患者信息
      total_price:'0.00',//处方金额合计
      patient_drug_select_table:[],//处方药品列表
      drug_search:{},//药品搜索数据
      view_search_drug_table:[],//显示药品搜索结果
    }    
  },
  mounted:function() {
    this.patient = JSON.parse(sessionStorage.getItem("record_now_patient"));
    if(isNullMessage(this,this.patient,'请先选择患者')){
      this.patient={
        real_name:'',
        case_number:'',
        age:'',
        gender:''
      }
    }
  },
}
</script>